package com.xgd.phylw.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgd.phylw.bean.Member;
import com.xgd.phylw.service.BackPageService;
import com.xgd.phylw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class MemberController {
    @Autowired
    private UserService userService;
    @Autowired
    private BackPageService backPageService;

    @RequestMapping("queryUser")
    public String queryUser( @SessionAttribute("user")Member name,Model model) {
        System.out.println(name);
        Member member = userService.queryMember(name);
        model.addAttribute("men",member);
        return "my-ck";
    }
    @RequestMapping("/changeUser")
    public String changeUser(Member member, @SessionAttribute("user")Member name, Model model
            , @RequestParam(required = false) MultipartFile img, HttpSession session) throws IOException {

        model.addAttribute("man",userService.queryMember(name));

        if(member.getMemberPhone()!=""&&member.getMemberName()!=""){

            if(img.isEmpty()){//默认头像
                member.setMemberHead(name.getMemberHead());
                int i = userService.changeMember(member,name.getMemberName());
                if (i==1){
                    model.addAttribute("myMsg","修改成功!");
                    name.setMemberName(member.getMemberName());
                    System.out.println("ok \t"+name);
                    return "redirect:queryUser";
                }else{
                    System.out.println("no \t"+name);
                    model.addAttribute("myMsg","修改失败");
                }
            }else{//"图像区域："

                String realPath =  session.getServletContext().getRealPath("");
                realPath +="/images/head/";
                String headPath = "images/head/"+img.getOriginalFilename();
                File file = new File(realPath + img.getOriginalFilename());

                member.setMemberHead(headPath);
                int i = userService.changeMember(member,name.getMemberName());
                if (i==1){
                    img.transferTo(file);
                    model.addAttribute("myMsg","修改成功!");
                    name.setMemberName(member.getMemberName());
                    System.out.println("ok \t"+name);
                    return "redirect:queryUser";
                }else{
                    System.out.println("no \t"+name);
                    model.addAttribute("myMsg","修改失败");
                }
            }
        }else{
            if(member.getMemberName()==""){
                member.setMemberName(name.getMemberName());
            }
            if(member.getMemberPhone()==""){
                member.setMemberPhone(name.getMemberPhone());
            }
            System.out.println("该用户未作任何修改");
        }

        return "my-xx";
    }

//后台跳转用户修改页面
    @RequestMapping("UserUpdate")
    public String PageChangeToUsersUpdate(String memberName,Model model){
        Member member = new Member();
        member.setMemberName(memberName);
        model.addAttribute("user",userService.queryMember(member));
        return "users_update";
    }
    @RequestMapping("UserByUpdate")
    public String UsersByUpdate(Member member,Model model,String name){
        int i = userService.changeMember(member,name);
        if(i==1){
            return "redirect:Users";
        }
        return "users_update";
    }
    @RequestMapping("UserDelete")
    public String PageChangeToUsersDelete(String[] names,Model model){
        System.out.println(names.length);
        for (String s:names) {
            System.out.println(s);
        }
        int i = userService.usersDelete(names);
        System.out.println(i);
        if (i==names.length){
            return "redirect:Users";
        }
        return "redirect:Users";
    }

    //后台用户模糊查询
    @RequestMapping("searchUser")
    public String PageChangeToSearchUser(String userName,Model model
            ,@RequestParam(value = "pageSize",required = false,defaultValue = "5") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){

        Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
        List<Member> members = userService.MemberAll(userName);

        for (Member m: members){
            System.out.println(m);
        }

        model.addAttribute("pages",objectPage.toPageInfo());
        model.addAttribute("members",members);
        return "users";
    }
}
